package com.sui.bigdata.sml.web.repository.mapper;

import com.sui.bigdata.sml.web.repository.model.Connection;
import com.sui.bigdata.sml.web.repository.model.NodeResult;
import com.sui.bigdata.sml.web.repository.model.Node;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author songhaicheng
 * @date 2019/11/5 10:00
 * @description
 * @reviewer
 */
@Repository
public interface NodeMapper {

    @Insert("insert into node(name,component_id,experiment_id,style,parameters,input,output,running_conf) " +
            "values(#{name},#{componentId},#{experimentId},#{style},#{parameters},#{input},#{output},#{runningConf})")
    @Options(useGeneratedKeys = true)
    int insert(Node node);

    @Select("select * from node where id=#{id}")
    Node get(Long id);

    @Select("select * from node where experiment_id=#{experimentId}")
    List<Node> listByExperimentId(Long experimentId);

    @Select("select * from node where component_id=#{componentId}")
    List<Node> listByComponentId(Long componentId);

    @Delete("delete from node where id=#{id}")
    int delete(Long id);

    @Update("<script>" +
            "<foreach collection='nodes' item='node' open='' separator=';' close=''>" +
            "update node set name=#{node.name},style=#{node.style},parameters=#{node.parameters}," +
            "status=#{node.status},running_conf=#{node.runningConf}" +
            "<if test='node.input!=null'>,input=#{node.input}</if>" +
            "<if test='node.output!=null'>,output=#{node.output}</if>" +
            " where id=#{node.id}" +
            "</foreach>" +
            "</script>")
    int updateBatch(@Param("nodes") List<Node> nodes);

    @Select("select * from connection where head_nid=#{id} or tail_nid=#{id}")
    List<Connection> listConnectionsByNodeId(Long id);

    @Select("<script>select * from connection where head_nid in" +
            "<foreach collection='nodeIds' item='nodeId' open='(' separator=',' close=')'>#{nodeId}</foreach></script>")
    List<Connection> listConnectionsBetweenNodeIds(@Param("nodeIds") List<Long> nodeIds);

    @Insert("insert into connection(head_nid,tail_nid,head_port_index,tail_port_index)" +
            " values(#{headNid},#{tailNid},#{headPortIndex},#{tailPortIndex})")
    @Options(useGeneratedKeys = true, keyColumn = "id")
    int insertConnection(Connection connection);

    @Delete("<script>delete from connection where id in (-666" +
            "<foreach collection='ids' item='id' open=',' separator=',' close=''>#{id}</foreach>)</script>")
    int deleteConnections(@Param("ids") List<Long> ids);

    @Select("SELECT id,node_id,cur_params,result,create_time FROM node_result WHERE node_id=#{id}" +
            " ORDER BY create_time DESC limit 1")
    NodeResult getResultById(Long id);

    @Select("SELECT id,create_time FROM node_result WHERE node_id=#{id} ORDER  BY create_time DESC LIMIT 10")
    List<NodeResult> getResultHistoriesById(Long id);

}
